package pojo

import (
	"fmt"
	"github.com/topfreegames/pitaya"
	"github.com/topfreegames/pitaya/fairy/constants"
	"github.com/topfreegames/pitaya/fairy/protos"
	"github.com/topfreegames/pitaya/fairy/util"
	"time"
)

const (
	RoleStateUse = 1
	RoleStateDelete = 2
	RoleStateLock = 3

	RoleStartLevel = 1//角色初始等级
	RoleStartGuideCopyId = 211//角色第一次创角时要进入的副本ID
	RoleStartSceneId = 2//角色初始场景ID
	RoleStartDirection = 45//角色初始朝向
)

type (
	PlayerData struct {
		BaseData `xorm:"-"`
		PlayerId int `xorm:"pk autoincr 'playerid' notnull"`
		AccountId int64 `xorm:"'accountId' default('0') notnull"`
		ChannelId string `xorm:"'channelId' default('') notnull"`
		BackSceneId int `xorm:"'backsceneid' default('0') notnull"`
		BackPosX int `xorm:"'backposX' default('0') notnull"`
		BackPosY int `xorm:"'backposY' default('0') notnull"`
		BackPosZ int `xorm:"'backposZ' default('0') notnull"`
		BagNum int `xorm:"'bagNum' default('72') notnull"`
		StoreNum int `xorm:"'storeNum' default('40') notnull"`
		BankMoney int `xorm:"'bankMoney' default('0') notnull"`
		BindBankMoney int `xorm:"'bindBankMoney' default('0') notnull"`
		CareerId int `xorm:"'careerid' default('0') notnull"`
		CloId int `xorm:"'cloId' default('0') notnull"`
		CreateTime time.Time `xorm:"'createTime' default('null')"`
		Direction int `xorm:"'direction' default('0') notnull"`
		PlayerExp int `xorm:"'playerexp' default('0') notnull"`
		GangId int64 `xorm:"'gangId' default('0') notnull"`
		Guid int64 `xorm:"'guid' default('0') notnull"`
		Hp int `xorm:"'hp' default('0') notnull"`
		LoginTime int64 `xorm:"'loginTime' default('0') notnull"`
		LogoutTime int64 `xorm:"'logoutTime' default('0') notnull"`
		MissionData []uint8 `xorm:"'missiondata' default('null')"`
		ActivityData       []uint8 `xorm:"'activityData' default('null')"`
		ChargeActivityData []uint8 `xorm:"'chargeactivityData' default('null')"`
		SevenDayActData    []uint8 `xorm:"'sevenDayActData' default('null')"`
		CardData           []uint8 `xorm:"'cardData' default('null')"`
		Money              int     `xorm:"'money' default('0') notnull"`
		Mp                 int     `xorm:"'mp' default('0') notnull"`
		PosX               int     `xorm:"'posX' default('0') notnull"`
		PosY               int     `xorm:"'posY' default('0') notnull"`
		PosZ               int     `xorm:"'posZ' default('0') notnull"`
		GmIngot            int     `xorm:"'gmIngot' default('0') notnull"`
		RoleIndex          int     `xorm:"'roleindex' default('0') notnull"`
		Rolelevel          int     `xorm:"'rolelevel' default('1') notnull"`
		RoleName           string  `xorm:"'rolename' default('') notnull"`
		AccountName        string  `xorm:"'accountname' default('') notnull"`
		SceneId            int     `xorm:"'sceneId' default('0') notnull"`
		SkillData          []uint8 `xorm:"'skillData' default('null')"`
		SkillPoint         int     `xorm:"'skillPoint' default('0') notnull"`
		EquipEssence       int     `xorm:"'equipEssence' default('0') notnull"`
		SoulEssence        int     `xorm:"'soulEssence' default('0') notnull"`
		CardEssence        int     `xorm:"'cardEssence' default('0') notnull"`
		ArenaMoney         int     `xorm:"'arenaMoney' default('0') notnull"`
		Xianyuan        int     `xorm:"'xianyuan' default('0') notnull"`
		Status          int     `xorm:"'status' default('0') notnull"`
		WeaId           int     `xorm:"'weaId' default('0') notnull"`
		WeaStarLevel    int     `xorm:"'weaStarLevel' default('0') notnull"`
		CampId          int16   `xorm:"'campId' default('0') notnull"`
		KillPoint       int16   `xorm:"'killPoint' default('0') notnull"`
		KillPointTime   int16   `xorm:"'killPointTime' default('0') notnull"`
		ChangeSceneTime int64   `xorm:"'changeSceneTime' default('0') notnull"`
		ChatTime        int64   `xorm:"'chatTime' default('0') notnull"`
		DeleteRoleTime  int64   `xorm:"'deleteRoleTime' default('0') notnull"`
		FabaoId         int     `xorm:"'fabaoId' default('0') notnull"`
		WingId          int     `xorm:"'wingId' default('0') notnull"`
		WingData []uint8 `xorm:"'wingData' default('null')"`
		TitleData []uint8 `xorm:"'titleData' default('null')"`
		TitleId int `xorm:"'titleId' default('0') notnull"`
		LastIp string `xorm:"'lastIp' default('') notnull"`
		ServerMailTime int64 `xorm:"'serverMailTime' default('0') notnull"`
		IngotShopData []uint8 `xorm:"'ingotShopData' default('null')"`
		JieYiName string `xorm:"'jieYiName' default('') notnull"`
		JieYiSubName string `xorm:"'jieYiSubName' default('') notnull"`
		JieYiTime int64 `xorm:"'jieYiTime' default('0') notnull"`
		SexId int8 `xorm:"'sexId' default('-1') notnull"`
		MarriageTime int64 `xorm:"'marriageTime' default('0') notnull"`
		MateName string `xorm:"'mateName' default('') notnull"`
		MateCareer int `xorm:"'mateCareer' default('0') notnull"`
		Prestige int `xorm:"'prestige' default('0') notnull"`
		WeddingState int16 `xorm:"'weddingState' default('0') notnull"`
		WeddingOrderTime int64 `xorm:"'weddingOrderTime' default('0') notnull"`
		WeddingGuestGuids []uint8 `xorm:"'weddingGuestGuids' default('null')"`
		BetFun []uint8 `xorm:"'betFun' default('null')"`
		MateGuid int64 `xorm:"'mateGuid' default('0') notnull"`
		BetIntegral int `xorm:"'betIntegral' default('0') notnull"`
		FashionId int `xorm:"'fashionId' default('0') notnull"`
		FashionData []uint8 `xorm:"'fashionData' default('null')"`
		TimecutData []uint8 `xorm:"'timecutData' default('null')"`
		AppId string `xorm:"'appId' default('') notnull"`
		WorldId int `xorm:"'worldId' default('0') notnull"`
	}
	
	PlayerDataSlice []*PlayerData
)

func (*PlayerData) TableName() string {
	return "playerdata"
}

func (p *PlayerData) GetDataKey() string {
	if p.Guid <= 0 {
		return ""
	}

	return fmt.Sprintf("player_data_%d",p.Guid)
}

func (p *PlayerDataSlice) Len() int {
	return len(*p)
}

/**
roleLevel越大 越靠前
 */
func (p *PlayerDataSlice) Less(i, j int) bool{
	return (*p)[i].Rolelevel < (*p)[j].Rolelevel
}
func (p *PlayerDataSlice) Swap(i, j int) {
	(*p)[i],(*p)[j] = (*p)[j],(*p)[i]
}

func (p *PlayerData) NewLcRoleInfoRet() *protos.LCRoleInfoRet {
	careerId := int32(p.CareerId)
	sexId := int32(p.SexId)
	roleIndex := int32(p.RoleIndex)
	roleLevel := int32(p.Rolelevel)
	playerId := int32(p.PlayerId)
	clothesId := int32(p.CloId)
	weaponId := int32(p.WeaId)
	weaStarLevel := int32(p.WeaStarLevel)
	fabaoId := int32(p.FabaoId)
	wingId := int32(p.WingId)
	fashionId := int32(p.FashionId)
	lastLoginTime := (int32)(p.LoginTime / 1000)
	deleteRoleTime := int64(0)
	if p.DeleteRoleTime > 0 {
		deleteRoleTime = (pitaya.GetConfig().GetDuration(constants.LoginServerDeleteRoleKeepTime).Milliseconds()-
			util.Now() + p.DeleteRoleTime)
	}
	createRoleTime := p.CreateTime.UnixNano() / 1e6


	return &protos.LCRoleInfoRet{
		PlayerId:        &playerId,
		RoleIndex:       &roleIndex,
		RoleName:        &p.RoleName,
		RoleLevel:       &roleLevel,
		CareerId:        &careerId,
		SexId:           &sexId,
		WeaponId:        &weaponId,
		WeaponStarLevel: &weaStarLevel,
		ClothesId:       &clothesId,
		WingId:          &wingId,
		FabaoId:         &fabaoId,
		FashionId:       &fashionId,
		LastLoginTime:   &lastLoginTime,
		DeleteRoleTime:  &deleteRoleTime,
		CreateRoleTime:  &createRoleTime,
	}
}

func (p *PlayerData)  GenerateMsgPlayerData() *protos.MsgPlayerData {
	worldId := int32(p.WorldId)
	backPosX := int32(p.PosX)
	backPosY := int32(p.PosY)
	backPosZ := int32(p.PosZ)
	backSceneId := int32(p.BackSceneId)
	bagNum := int32(p.BagNum)
	bankMoney := int32(p.BankMoney)
	bindBankMoney := int32(p.BindBankMoney)
	campId := int32(p.CampId)
	careeId := int32(p.CareerId)
	sexId := int32(p.SexId)
	changeSceneTime := p.ChangeSceneTime
	chatTime := p.ChatTime

	cloId := int32(p.CloId)
	createTime := p.ChatTime
	deleteRoleTime := p.DeleteRoleTime
	direction := int32(p.Direction)
	equipEssence := int32(p.EquipEssence)
	cardEssence := int32(p.CardEssence)
	soulEssence := int32(p.SoulEssence)
	arenaMoney := int32(p.ArenaMoney)
	xianYuan := int32(p.Xianyuan)
	fabaoId := int32(p.FabaoId)
	gangId := p.GangId
	gmingot := int32(p.GmIngot)
	guid := p.Guid

	hp := int32(p.Hp)
	killPoint := int32(p.KillPoint)
	killPointTime := int32(p.KillPointTime)
	lastIp := p.LastIp
	loginTime := p.LoginTime
	logoutTime := p.LogoutTime
	mp := int32(p.Mp)
	money := int32(p.Money)
	playerId := int32(p.PlayerId)
	playerExp := int32(p.PlayerExp)
	posX := int32(p.PosX)
	posY := int32(p.PosY)
	posZ := int32(p.PosZ)
	roleIndex := int32(p.RoleIndex)
	roleLevel := int32(p.Rolelevel)
	roleName := p.RoleName
	sceneId := int32(p.SceneId)
	storeNum := int32(p.StoreNum)
	skillPoint := int32(p.SkillPoint)

	status := int32(p.Status)
	titleId := int32(p.TitleId)
	weaId := int32(p.WeaId)
	weaStarLevel := int32(p.WeaStarLevel)
	wingId := int32(p.WingId)
	serverMailTime := p.ServerMailTime
	channelId := p.ChannelId
	jieYiName := p.JieYiName
	jieYiSubName := p.JieYiSubName
	jieYiTime := p.JieYiTime
	mateName := p.MateName
	mateCareer := int32(p.MateCareer)
	marriageTime := p.MarriageTime
	prestige := int32(p.Prestige)
	weddingState := int32(p.WeddingState)
	weddingOrderTime := p.WeddingOrderTime
	mateGuidId := p.MateGuid
	betIntergral := int32(p.BetIntegral)
	fashionId := int32(p.FashionId)


	msgPlayerData := &protos.MsgPlayerData{
		AccountId:&p.AccountId,
		AppId:&p.AppId,
		World:&worldId,
		Accountname:&p.AccountName,
		BackposX:&backPosX,
		BackposY:&backPosY,
		BackposZ:&backPosZ,
		BackSceneID:&backSceneId,
		BagNum:&bagNum,
		BankMoney:&bankMoney,
		BindBankMoney:&bindBankMoney,
		CampId:&campId,
		CareerId:&careeId,
		SexId:&sexId,
		ChangeSceneTime:&changeSceneTime,
		ChatTime:&chatTime,
		CloId:&cloId,
		CreateTime:&createTime,
		DeleteRoleTime:&deleteRoleTime,
		Direction:&direction,
		EquipEssence:&equipEssence,
		CardEssence:&cardEssence,
		SoulEssence:&soulEssence,
		ArenaMoney:&arenaMoney,
		Xianyuan:&xianYuan,
		FabaoId:&fabaoId,
		GangId:&gangId,
		GmIngot:&gmingot,
		Guid:&guid,
		Hp:&hp,
		KillPoint:&killPoint,
		KillPointTime:&killPointTime,
		LastIp:&lastIp,
		Logintime:&loginTime,
		LogoutTime:&logoutTime,
		Mp:&mp,
		Money:&money,
		PlayerId:&playerId,
		Playerexp:&playerExp,
		PosX:&posX,
		PosY:&posY,
		PosZ:&posZ,
		RoleIndex:&roleIndex,
		RoleLevel:&roleLevel,
		RoleName:&roleName,
		SceneId:&sceneId,
		StoreNum:&storeNum,
		SkillPoint:&skillPoint,
		Status:&status,
		TitleId:&titleId,
		WeaId:&weaId,
		WeaStarLevel:&weaStarLevel,
		WingId:&wingId,
		ServerMailTime:&serverMailTime,
		ChannelId:&channelId,
		JieYiName:&jieYiName,
		JieYiSubName:&jieYiSubName,
		JieYiTime:&jieYiTime,
		MateName:&mateName,
		MateCareer:&mateCareer,
		MarriageTime:&marriageTime,
		Prestige:&prestige,
		WeddingState:&weddingState,
		WeddingOrderTime:&weddingOrderTime,
		MateGuid:&mateGuidId,
		BetIntegral:&betIntergral,
		FashionId:&fashionId,
	}

	if p.MissionData != nil && len(p.MissionData) > 0 {
		msgPlayerData.MissionData = util.ByteArrayToIntArray(p.MissionData)
	}

	if p.ActivityData != nil && len(p.ActivityData) > 0 {
		msgPlayerData.ActivityData = util.ByteArrayToIntArray(p.ActivityData)
	}

	if p.ChargeActivityData != nil && len(p.ChargeActivityData) > 0 {
		msgPlayerData.ChargeActivityData = util.ByteArrayToIntArray(p.ChargeActivityData)
	}

	if p.SevenDayActData != nil && len(p.SevenDayActData) > 0 {
		msgPlayerData.SevenDayActData = util.ByteArrayToIntArray(p.SevenDayActData)
	}

	if p.TimecutData != nil && len(p.TimecutData) > 0 {
		msgPlayerData.TimecutData = util.ByteArrayToIntArray(p.TimecutData)
	}

	if p.CardData != nil && len(p.CardData) > 0 {
		msgPlayerData.CardData = util.ByteArrayToIntArray(p.CardData)
	}

	if p.SkillData != nil && len(p.SkillData) > 0 {
		msgPlayerData.SkillData = util.ByteArrayToIntArray(p.SkillData)
	}

	if p.WingData != nil && len(p.WingData) > 0 {
		msgPlayerData.WingData = util.ByteArrayToIntArray(p.WingData)
	}

	if p.TitleData != nil && len(p.TitleData) > 0 {
		msgPlayerData.TitleData = util.ByteArrayToIntArray(p.TitleData)
	}

	if p.IngotShopData != nil && len(p.IngotShopData) > 0 {
		msgPlayerData.IngotShopData = util.ByteArrayToIntArray(p.IngotShopData)
	}

	if p.WeddingGuestGuids != nil && len(p.WeddingGuestGuids) > 0 {
		msgPlayerData.WeddingInvitationData = util.ByteArrayToIntArray(p.WeddingGuestGuids)
	}

	if p.BetFun != nil && len(p.BetFun) > 0 {
		msgPlayerData.BetFunData = util.ByteArrayToIntArray(p.BetFun)
	}

	if p.FashionData != nil && len(p.FashionData) > 0 {
		msgPlayerData.FashionData = util.ByteArrayToIntArray(p.FashionData)
	}

	return msgPlayerData
}